//
//  TJTaskManagerDetailController.m
//  BaoDian
//
//  Created by mac on 2024/8/3.
//

#import "TJTaskManagerDetailController.h"
#import "TJTaskManagerDetailCell.h"
#import "TJEmptyDataHandler.h"
#import "TJModifyPOIController.h"
#import "TJTaskManagerDetailHeaderView.h"
#import "TJVideoPlayController.h"

@interface TJTaskManagerDetailController ()

@property (nonatomic, strong) TJEmptyDataHandler *emptyDataHandler;
@property (nonatomic, strong) NSMutableArray *datas;

@property (nonatomic, strong) CAShapeLayer *topShapeLayer;
@property (nonatomic, strong) CAShapeLayer *bottomShapeLayer;



@end

@implementation TJTaskManagerDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    TJTaskManagerDetailHeaderView *headerView = [[TJTaskManagerDetailHeaderView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth - 32, 85 + 320)];
    headerView.model = self.model;
    self.tableView.tableHeaderView = headerView;
    [self.tableView registerClass:TJTaskManagerDetailCell.class forCellReuseIdentifier:@"TJTaskManagerDetailCell"];
    self.tableView.separatorColor = UIColorMake(0x2A2B31);
    self.tableView.rowHeight = 210;
    self.tableView.backgroundColor = UIColor.clearColor;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(KNavBarHight);
        make.left.right.inset(16);
        make.bottom.offset(0);
    }];
    
    _emptyDataHandler = [TJEmptyDataHandler emptyDataHandleWithScrollView:self.tableView configuration:^TJEmptyDataType(TJEmptyDataModel * _Nonnull dataModel) {
        dataModel.text = @"你暂时还没有创建分组";
        dataModel.iconName = @"clockInRecardEmpty";
        return TJEmptyDataTypeBase;
    }];
    _emptyDataHandler.addOffsetY =  30;
    
    [self loadData];
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TJTaskManagerDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TJTaskManagerDetailCell" forIndexPath:indexPath];
    
    TJTaskModel *model = self.datas[indexPath.row];
    cell.model = model;
    TJWeakify(self)
    [cell setTapCoverImageViewBlock:^(UIImage *coverImage){
        TJVideoPlayController *videoPlayController = [[TJVideoPlayController alloc] init];
        videoPlayController.url = model.videoUrl;
        videoPlayController.coverImage = coverImage;
        [weakself presentViewController:videoPlayController animated:YES completion:nil];
    }];
    
    if (indexPath.row == 0) {
        cell.layer.mask = [self topShapeLayerWithView:cell];
    } else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section] - 1) {
        cell.layer.mask = [self bottomShapeLayerWithView:cell];
    } else {
        cell.layer.mask = nil;
    }
    return cell;
}


- (UIImage *)topBackgroundImage {
    return [UIImage imageNamed:@"taskDetail"];
}
 
- (CAShapeLayer *)topShapeLayerWithView:(UIView *)view {
    if (!_topShapeLayer) {
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(12, 12)];
        _topShapeLayer = [CAShapeLayer layer];
        _topShapeLayer.path = maskPath.CGPath;
    }
    return _topShapeLayer;
}

- (CAShapeLayer *)bottomShapeLayerWithView:(UIView *)view {
    if (!_bottomShapeLayer) {
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(12, 12)];
        _bottomShapeLayer = [CAShapeLayer layer];
        _bottomShapeLayer.path = maskPath.CGPath;
    }
    return _bottomShapeLayer;
}

- (NSMutableArray *)datas {
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}

- (void)loadData {
    [TJMinePresent loadTaskDetailWithTaskId:self.model.taskId complete:^(NSArray * _Nonnull datas, NSString *remainTime, NSError * _Nullable error) {
        if (datas.count) {
            [self.datas removeAllObjects];
            [self.datas addObjectsFromArray:datas];
        }
        self.emptyDataHandler.canDisplay = YES;
        [self.tableView reloadData];
    }];
}


@end
